Skip to main content

Sorting data

Basic sorting

  1. Default behavior of the return is to simply return the results in the order the were returned or consolidated

    for airport in airports
    return airport
  2. Especially in a distributed database there is no guarantee of the order in which the results will be returned. Here is a sample of the unsorted results

    [
    {
    "_key": "00M",
    "_id": "airports/00M",
    "_rev": "_gdnKCgi---",
    "name": "Thigpen ",
    "city": "Bay Springs",
    "state": "MS",
    "country": "USA",
    "lat": 31.95376472,
    "long": -89.23450472,
    "vip": false
    },
    {
    "_key": "00R",
    "_id": "airports/00R",
    "_rev": "_gdnKCgi--_",
    "name": "Livingston Municipal",
    "city": "Livingston",
    "state": "TX",
    "country": "USA",
    "lat": 30.68586111,
    "long": -95.01792778,
    "vip": false
    },
    {
    "_key": "00V",
    "_id": "airports/00V",
    "_rev": "_gdnKCgi--A",
    "name": "Meadow Lake",
    "city": "Colorado Springs",
    "state": "CO",
    "country": "USA",
    "lat": 38.94574889,
    "long": -104.5698933,
    "vip": false
    },
  3. A sort orders the results by a particular field or attribute

    for airport in airports
    sort airport.city
    return airport
  4. Here is a snippet of the expected results

    [
    {
    "_key": "0R3",
    "_id": "airports/0R3",
    "_rev": "_gdnKCgm-_L",
    "name": "Abbeville Chris Crusta Memorial",
    "city": "Abbeville",
    "state": "LA",
    "country": "USA",
    "lat": 29.97576083,
    "long": -92.08415167,
    "vip": false
    },
    {
    "_key": "0J0",
    "_id": "airports/0J0",
    "_rev": "_gdnKCgm--3",
    "name": "Abbeville Municipal",
    "city": "Abbeville",
    "state": "AL",
    "country": "USA",
    "lat": 31.60016778,
    "long": -85.23882222,
    "vip": false
    },
    {
    "_key": "U36",
    "_id": "airports/U36",
    "_rev": "_gdnKCiq--X",
    "name": "Aberdeen Municipal",
    "city": "Aberdeen",
    "state": "ID",
    "country": "USA",
    "lat": 42.92102222,
    "long": -112.8811053,
    "vip": false
    },
    {
    "_key": "ABR",
    "_id": "airports/ABR",
    "_rev": "_gdnKChC-_a",
    "name": "Aberdeen Regional",
    "city": "Aberdeen",
    "state": "SD",
    "country": "USA",
    "lat": 45.44905556,
    "long": -98.42183333,
    "vip": false
    },
  5. You can sort by multiple keys; lets sort by state first (ascending) and then city (descending)

    for airport in airports
    sort airport.state asc, airport.city desc
    return airport
  6. Here is a snippet of the expected results

    [
    {
    "_key": "YAK",
    "_id": "airports/YAK",
    "_rev": "_gdnKCiy--s",
    "name": "Yakutat",
    "city": "Yakutat",
    "state": "AK",
    "country": "USA",
    "lat": 59.50336056,
    "long": -139.6602261,
    "vip": false
    },
    {
    "_key": "2Y3",
    "_id": "airports/2Y3",
    "_rev": "_gdnKCgy--A",
    "name": "Yakutat SPB",
    "city": "Yakutat",
    "state": "AK",
    "country": "USA",
    "lat": 59.5624775,
    "long": -139.7410994,
    "vip": false
    },
    {
    "_key": "68A",
    "_id": "airports/68A",
    "_rev": "_gdnKCg6-_f",
    "name": "Wrangell SPB",
    "city": "Wrangell",
    "state": "AK",
    "country": "USA",
    "lat": 56.466325,
    "long": -132.3800181,
    "vip": false
    },
    {
    "_key": "WRG",
    "_id": "airports/WRG",
    "_rev": "_gdnKCiu-_M",
    "name": "Wrangell",
    "city": "Wrangell",
    "state": "AK",
    "country": "USA",
    "lat": 56.48432583,
    "long": -132.3698242,
    "vip": false
    },
 
Help us improve

Anything unclear or buggy in this tutorial? Provide Feedback